package org.itboys.mobile.service.mongo.weixin;

import org.apache.commons.collections.map.HashedMap;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.comment.SundayCommentDto;
import org.itboys.mobile.entity.mongo.SundayComment;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/16
 * @Description：评论service
 */
@Service
public class SundayWeiXinCommentService extends BaseMongoService<SundayComment>{
    @Resource(name = "mobileDs")
    private MongoDataSource yefuDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return yefuDataSource;
    }

    @Override
    protected Class<SundayComment> getEntityClass() {
        return SundayComment.class;
    }

    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayProductService productService;
    @Autowired
    private ResourceHolder resourceHolder;

    /**
     * 保存评论
     * @param memberId
     * @param productId
     * @param rank
     * @param identity
     * @param content
     */
    public void save(Long memberId,Long productId,Integer rank,String identity,String content){
        SundayComment comment=new SundayComment(productId,identity,content,rank,memberId,SundayCommonConstants.comment_type_unchecked);
        super.save(comment);
    }

    /**
     * 分页查询评论,查询个人评论||产品评论
     * @param request
     * @return
     */
    public List<SundayCommentDto> select(HttpServletRequest request,Long memberId,Long productId){
        List<SundayCommentDto> result=new ArrayList<>();
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request, "productId","memberId");
        if(LongUtil.isNotZreo(productId)){
            param.put("status",SundayCommonConstants.comment_type_pass);
        }
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("sort","ct");
        param.put("order","desc");
        List<SundayComment> data = super.pageQuery(request,param).getData();
        if(ListUtil.isNull(data)){
            return result;
        }
        Map<String,Object> _param=new HashedMap();
        _param.put("isDeleted",SundayCommonConstants.NO);
        List<Long> ids=new ArrayList<>();
        if(LongUtil.isNotZreo(memberId)){
            /**
             * 根据用户id查询用户点评
             */
            SundayMember member=memberService.getById(memberId);
            for (SundayComment comment:data){
                ids.add(comment.getProductId());
            }
            /**
             * 获取所有的楼盘
             */
            _param.put("ids",ids);
            List<SundayProduct> products = productService.select(_param);
            for (SundayComment comment:data) {
                for (SundayProduct product:products) {
                    if (comment.getProductId().equals(product.getId())){
                        SundayCommentDto commentDto = packageDto(comment, product, member);
                        result.add(commentDto);
                        break;
                    }
                }
            }
        }else if(LongUtil.isNotZreo(productId)){
            /**
             * 获取楼盘点评
             */
            SundayProduct product=productService.find(productId);
            for (SundayComment comment:data){
                ids.add(comment.getMemberId());
            }
            /**
             * 获取所有的楼盘，楼盘点评未通过的评论不能展示
             */
            _param.put("isDeleted", SundayCommonConstants.NO);
            _param.put("id in",ids);
            List<SundayMember> members = memberService.list(_param);
            for (SundayComment comment:data) {
                for (SundayMember member:members) {
                    if (comment.getMemberId().equals(member.getId())){
                        SundayCommentDto commentDto = packageDto(comment, product, member);
                        result.add(commentDto);
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 封装返回结果dto
     * @param comment
     * @param product
     * @param member
     * @return
     */
    public SundayCommentDto packageDto(SundayComment comment, SundayProduct product, SundayMember member){
        SundayCommentDto result=new SundayCommentDto();
        result.setId(comment.getId());
        result.setMemberId(comment.getMemberId());
        result.setMemberImage(resourceHolder.getStringValue("imgRoot")+member.getImage());
        result.setContent(comment.getContent());
        result.setDate(comment.getCreateDate());
        result.setProductId(product.getId());
        result.setIdentity(comment.getIdentity());
        result.setProductName(product.getName());
        result.setProductImage(resourceHolder.getStringValue("imgRoot")+product.getBackImage());
        result.setMemberName(member.getNickName());
        result.setRank(comment.getRank());
        result.setStatus(comment.getStatus());
        return  result;
    }
}
